package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketLog;
import com.cskaoyan.bean.po.MarketLogExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.chaosbean.log.vo.LogVO;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.service.adminservice.LogService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author jackX
 * @datetime 2022/12/29 15:03
 * @description: TODO:
 */
@Component
public class LogServiceImpl implements LogService {
    @Autowired
    MarketLogMapper logMapper;

    @Override
    public LogVO listLog(BasePageInfo info, String name) {
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        LogVO logVO = new LogVO();
        MarketLogExample example = new MarketLogExample();
        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketLogExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andAdminLike("%" + name + "%");
        }
        List<MarketLog> list = logMapper.selectByExample(example);
        logVO.setList(list);
        PageInfo<MarketLog> pageInfo = new PageInfo<>(list);
        logVO.setLimit(pageInfo.getPageSize());//limit
        logVO.setPage(pageInfo.getPageNum());//page
        logVO.setPages(pageInfo.getPages());//pages
        logVO.setTotal((int) pageInfo.getTotal());//total
        return logVO;
    }

    @Override
    public void createLog(MarketLog marketLog) {
        logMapper.insert(marketLog);
    }
}
